package com.zjw.zy.componse.demo.fragment

import android.util.Log
import android.widget.Toast
import androidx.compose.foundation.layout.*
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import com.zjw.zy.componse.demo.R
import com.zjw.zy.componse.demo.base.BaseComposeFragment
import com.zjw.zy.componse.demo.bean.User
import com.zjw.zy.componse.demo.ext.go
import com.zjw.zy.componse.demo.ui.theme.MyApplicationTheme
import com.zjw.zy.componse.demo.view.CommonEditView
import com.zjw.zy.componse.demo.viewmodel.LoginViewModel

/**
 * @author     ：zhong.jw
 * @date       ：Created in 2023/2/16 11:24
 * @description：登录Fragment
 */
class LoginFragment : BaseComposeFragment() {


    private val loginViewModel: LoginViewModel by viewModels()

    override fun onContentView(rootView: ComposeView) {
        rootView.setContent {
            MyApplicationTheme {
                LoginView()
            }
        }
    }

    override fun onSubscribeValue() {
        lifecycleScope.launchWhenResumed {
            loginViewModel.loginStatus.collect {
                if (it) {
                    Toast.makeText(requireContext(), "登录成功", Toast.LENGTH_SHORT).show()
                    go(R.id.dogListFragment)
                } else {
                    Toast.makeText(requireContext(), "用户名或密码错误", Toast.LENGTH_SHORT).show()
                }
            }
        }
    }

    @Composable
    @Preview
    fun LoginView() {

        val username = remember { mutableStateOf("") }
        val password = remember { mutableStateOf("") }

        Box(modifier = Modifier.fillMaxSize()) {
            Column(
                modifier = Modifier
                    .padding(10.dp)
                    .wrapContentSize()
                    .align(Alignment.Center),
                horizontalAlignment = Alignment.CenterHorizontally
            ) {

                CommonEditView(
                    value = username.value,
                    hint = "用户名",
                    onValueChange = {
                        username.value = it
                    },
                    modifier = Modifier.height(50.dp),

                    leftIcon = R.drawable.icon_edit
                )

                Spacer(modifier = Modifier.height(10.dp))

                CommonEditView(
                    value = password.value,
                    hint = "密码",
                    onValueChange = {
                        password.value = it
                    },
                    leftIcon = R.drawable.icon_edit,
                    modifier = Modifier.height(50.dp)
                )

                Button(onClick = {
                    loginViewModel.requestLogin(User(username.value, password.value))
                    Log.d("test", "LoginView: $username $password")
                }) {
                    Text(text = "登录")
                }
            }
        }
    }
}

